<?php

namespace common\models\tsline;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%ts_channel}}".
 *
 * @property int $id
 * @property string $name 频道名称
 * @property int $status 状态
 * @property int $listorder 排序
 * @property int $editable 是否可编辑的
 * @property int $recommend 是否默认推荐
 * @property string $link
 * @property string $fun 调用方法
 * @property string $template 模板
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class TslineChannel extends \yii\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;

    /**
     * 状态
     * 0关闭，1开启，-1删除
     */
    CONST STATUS_DEFAULT = 0;
    CONST STATUS_ON = 1;
    CONST STATUS_DELETED = -1;
    CONST EDITABLE_ON = 1;
    CONST EDITABLE_OFF = 0;
    const TEMPLATE_DEFAULT = 'show';
    const FUN_DEFAULT = 'detail';

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%ts_channel}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['status', 'listorder', 'editable', 'recommend', 'created_at', 'updated_at'], 'integer'],
            [['name', 'template'], 'string', 'max' => 30],
            [['link'], 'string', 'max' => 128],
            [['fun'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '频道名称',
            'status' => '状态',
            'listorder' => '排序',
            'editable' => '是否可编辑的',
            'recommend' => '是否默认推荐',
            'link' => 'Link',
            'fun' => '调用方法',
            'template' => '模板',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public static function getList() {
        $res = static::find()->select(['id', 'name'])->all();
        return ArrayHelper::map($res, 'id', 'name');
    }

    public static function activeList($bloger_id) {
        $channel = static::find()->select(['id', 'name'])
        ->where([
            'AND',
            ['status' => static::STATUS_ON],
            ['in', 'id', (new \yii\db\Query())
                ->select('channel_id')
                ->from(TsBlogerChannel::tableName())
                ->where('bloger_id=' . $bloger_id)]
        ])->all();
        return ArrayHelper::map($channel, 'id', 'name');
    }

}
